package pro.cherish.smartfile.common.response;

import pro.cherish.smartfile.common.enums.StatusEnum;

import java.io.Serializable;

public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private boolean success;
    private String message;
    private Object messageObj;
    private String messageType;
    private String code;
    private T data;
    private long timestamp = System.currentTimeMillis();

    private Result() {
    }

    public static Result ok() {
        Result r = new Result();
        r.setSuccess(true);
        r.setMessage("OK");
        r.setCode("200");
        return r;
    }

    public static <T> Result<T> ok(StatusEnum statusEnum, T data) {
        Result<T> r = new Result();
        r.setSuccess(true);
        r.setData(data);
        r.setMessage(statusEnum.getDesc());
        r.setMessageObj(statusEnum.getStatus());
        r.setCode("200");
        return r;
    }

    public static Result<Object> error(StatusEnum statusEnum) {
        Result<Object> r = new Result();
        r.setCode(statusEnum.getCode());
        r.setMessage(statusEnum.getDesc());
        r.setMessageObj(statusEnum.getStatus());
        r.setSuccess(false);
        return r;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> r = new Result();
        r.setSuccess(true);
        r.setMessage("OK");
        r.setCode("200");
        r.setData(data);
        return r;
    }

    public static Result<Object> error(String code, String msg) {
        Result<Object> r = new Result();
        r.setCode(code);
        r.setMessage(msg);
        r.setSuccess(false);
        return r;
    }

    public static Result<Object> error(String msg) {
        return error("500", msg);
    }

    public static Result<Object> noauth(String msg) {
        return error("510", msg);
    }

    public boolean isSuccess() {
        return this.success;
    }

    public void setSuccess(final boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return this.message;
    }

    public void setMessage(final String message) {
        this.message = message;
    }

    public Object getMessageObj() {
        return this.messageObj;
    }

    public void setMessageObj(Object messageObj) {
        this.messageObj = messageObj;
    }

    public String getMessageType() {
        return this.messageType;
    }

    public void setMessageType(final String messageType) {
        this.messageType = messageType;
    }

    public String getCode() {
        return this.code;
    }

    public void setCode(final String code) {
        this.code = code;
    }

    public T getData() {
        return this.data;
    }

    public void setData(final T data) {
        this.data = data;
    }

    public long getTimestamp() {
        return this.timestamp;
    }

    public void setTimestamp(final long timestamp) {
        this.timestamp = timestamp;
    }


    protected boolean canEqual(final Object other) {
        return other instanceof Result;
    }


    @Override
    public String toString() {
        return "Result(success=" + this.isSuccess() + ", message=" + this.getMessage() + ", messageType=" + this.getMessageType() + ", code=" + this.getCode() + ", data=" + this.getData() + ", timestamp=" + this.getTimestamp() + ")";
    }
}
